﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simulate_control.Basic
{
    #region USING
    using simulate_control.Repositories;
    using simulate_control.Models;
    using simulate_control.Helpers;
    using DevComponents.DotNetBar.Controls;
    #endregion

    public partial class FrmWayPoint : Form
    {
        #region VARIABLES
        public EventHandler Handler { get; set; }
        private Dictionary<int, string> dict = new Dictionary<int, string>();

        // Bien danh dau hien thoi dang lua chon item nao
        private int Selected = 0;

        // Bien danh dau item da di vao trang thai edit hay chua
        private bool EnterEdit = false;

        // Ki tu trong item duoc lua chon 
        private int SelectedCharacterPos = 0;

        // 3 mang dung de phan loai item thuoc ve nhom nao: time, character hay value
        private string[] arrTimeVal = new string[] { };
        private string[] arrChar = new string[] { "lblName", "lblDate", "lblTimeMode" };
        private string[] arrVal = new string[] { "lblN", "lblE", "lblTime" };

        // Bien chua dung danh sach ten cac label item
        private string[] lstHighlightLabelName = new string[] { "lblName", "lblN", "lblE", "lblDate", "lblTime", "lblTimeMode" };
        #endregion

        public FrmWayPoint()
        {
            InitializeComponent();
        }

        public void ReInitLabels(string[] lbls)
        {
            foreach (var str in lbls)
            {
                var lbl = Utils.GetSelectedHighlightLabel(pnlMain, str);

                switch (str)
                {
                    case "lblN":
                        lbl.LabelText = "00" + "\u00b0" + "00.000" + "'N";
                        lbl.LabelMaxLength = 9;
                        break;

                    case "lblE":
                        lbl.LabelText = "00" + "\u00b0" + "00.000" + "'E";
                        lbl.LabelMaxLength = 9;
                        break;

                    case "lblDate":
                        lbl.LabelText = "01-APR";
                        lbl.LabelMaxLength = lbl.LabelText.Length;
                        break;

                    case "lblTime":
                        lbl.LabelText = "00:00";
                        lbl.LabelMaxLength = lbl.LabelText.Length;
                        break;

                    case "lblTimeMode":
                        lbl.LabelText = "AMD";
                        lbl.LabelMaxLength = lbl.LabelText.Length;
                        break;
                }
            }
        }

        /// <summary>
        /// Khoi tao danh sach cac highlight labels
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="lstHighlightLabelName"></param>
        public void InitHighlightLabels(ref GroupPanel pnlMain, string[] lstHighlightLabelName)
        {
            for (int i = 0; i < lstHighlightLabelName.Length; i++)
            {
                var lbl = new HighlightLabel();
                InitLabel(ref pnlMain, lbl, lstHighlightLabelName[i], Utils.CreateBlankString(10));
            }
        }

        /// <summary>
        /// Khoi tao 1 highlight label
        /// </summary>
        /// <param name="pnlMain"></param>
        /// <param name="lbl"></param>
        /// <param name="name"></param>
        /// <param name="text"></param>
        /// <param name="y"></param>
        public void InitLabel(ref GroupPanel pnlMain, HighlightLabel lbl, string name, string text)
        {
            lbl.LabelText = text;
            lbl.BackColor = System.Drawing.Color.Transparent;
            lbl.Name = name;
            lbl.TextAlignment = System.Drawing.StringAlignment.Center;
            lbl.HighlightPositions = new int[] { };
            lbl.LabelMaxLength = text.Length;
            //lbl.LabelSelectedFont = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(163)));
            
            switch (lbl.Name)
            {
                case "lblName":
                    lbl.Size = new System.Drawing.Size(110, 23);
                    lbl.Location = new System.Drawing.Point(101, 26);
                    break;
                case "lblN":
                    lbl.Size = new System.Drawing.Size(110, 23);
                    lbl.Location = new System.Drawing.Point(31, 65);
                    break;
                case "lblE":
                    lbl.Size = new System.Drawing.Size(110, 23);
                    lbl.Location = new System.Drawing.Point(31, 102);
                    break;
                case "lblDate":
                    lbl.Size = new System.Drawing.Size(75, 23);
                    lbl.Location = new System.Drawing.Point(35, 149);
                    break;
                case "lblTime":
                    lbl.Size = new System.Drawing.Size(75, 23);
                    lbl.Location = new System.Drawing.Point(180, 149);
                    break;
                case "lblTimeMode":
                    lbl.Size = new System.Drawing.Size(75, 23);
                    lbl.Location = new System.Drawing.Point(318, 149);
                    break;
            }

            pnlMain.Controls.Add(lbl);
        }

        public void ProcessKeyEvent(KEY keyEvent)
        {
            // Da di vao trang thai edit
            if (EnterEdit)
            {
                // Nhan enter de cap nhat gia tri, va thoat khoi trang thai edit
                if (keyEvent == KEY.ENTER)
                {
                    Utils.RetrieveToNextItemPos(pnlMain, dict, ref Selected);
                    // Thoat khoi trang thai edit
                    EnterEdit = false;
                }
                else
                {
                    // Cap nhat lai gia tri cho item duoc lua chon
                    var lbl = Utils.GetSelectedHighlightLabel(pnlMain, dict[Selected]);
                    Utils.ChangeCharacterPos(ref lbl, dict, Selected, ref SelectedCharacterPos, keyEvent, arrTimeVal, arrChar, arrVal);
                }
            }
            else // Chua di vao trang thai edit
            {
                // Nhan enter de di vao trang thai edit
                if (keyEvent == KEY.ENTER)
                {
                    // Exit form
                    if (dict[Selected] == "lblExit")
                    {
                        // Luu waypoint 
                        var item = new WayPoint
                        {
                            Name = Utils.GetSelectedHighlightLabel(pnlMain, "lblName").LabelText,
                            NCordinate = Utils.GetSelectedHighlightLabel(pnlMain, "lblN").LabelText,
                            ECordinate = Utils.GetSelectedHighlightLabel(pnlMain, "lblE").LabelText,
                            Mark = lblMark.Text,
                            Date = Utils.GetSelectedHighlightLabel(pnlMain, "lblDate").LabelText,
                            Time = Utils.GetSelectedHighlightLabel(pnlMain, "lblTime").LabelText,
                            TimeMode = Utils.GetSelectedHighlightLabel(pnlMain, "lblTimeMode").LabelText,
                        };

                        WayPointRepository.Insert(item);
                        this.Handler(this, new TransferDataEvent("EXIT"));
                        this.Close();
                    }
                    else if(dict[Selected] == "lblLog")
                    {
                        this.Handler(this, new TransferDataEvent("LOG"));
                        this.Close();
                    }
                    else
                    {
                        var lbl = Utils.GetSelectedHighlightLabel(pnlMain, dict[Selected]);
                        SelectedCharacterPos = 0;
                        Utils.UpdateHighlightLabels(ref lbl, SelectedCharacterPos);
                        // Di vao trang thai edit
                        EnterEdit = true;
                    }
                }
                else
                {
                    // Lua chon item de edit
                    Utils.ChangeSelectItemPos(pnlMain, dict, ref Selected, keyEvent);
                }
            }
        }

        public void InitDict()
        {
            dict.Add(0, "lblName");
            dict.Add(1, "lblN");
            dict.Add(2, "lblE");
            dict.Add(3, "lblMark");
            dict.Add(4, "lblDate");
            dict.Add(5, "lblTime");
            dict.Add(6, "lblTimeMode");
            dict.Add(7, "lblExit");
            dict.Add(8, "lblLog");
            dict.Add(9, "lblMob");
        }

        private void FrmWayPoint_Load(object sender, EventArgs e)
        {
            // Init Time diff
            InitHighlightLabels(ref pnlMain, lstHighlightLabelName);

            InitDict();

            Utils.ChangeSelectItem(pnlMain, dict, 0);

            string[] reInitLbls = new string[] { "lblN", "lblE", "lblMark", "lblDate", "lblTime", "lblTimeMode" };
            ReInitLabels(reInitLbls);
        }
    }
}
